﻿@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <link href="~/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
    @Html.AntiForgeryToken()
    <div class="layui-form" style="padding: 20px 30px 0 0;">
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-block">
                <input type="text" name="roleName" lay-verify="required|roleName" placeholder="请输入角色名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="启用" checked>
                <input type="radio" name="status" value="0" title="禁用">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="roleForm">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </div>

    <script src="~/scripts/layui-v2.5.6/layui/layui.js"></script>
    <script>
        layui.use(['form', 'layer', 'jquery'], function(form, layer, $){
            var form = layui.form;
            var layer = layui.layer;

            // 自定义验证规则
            form.verify({
                roleName: function(value) {
                    if(value.length === 0) {
                        return '角色名称不能为空';
                    }
                }
            });

            // 监听提交
            form.on('submit(roleForm)', function(data){
                var field = data.field;
                // 构建要发送的JSON对象，使键名与后端RoleDto属性名一致
                var m = {
                    RoleName: field.roleName,
                    RoleStatus: parseInt(field.status),
                    __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val()
                };

                $.ajax({
                    url: '/Role/AddRole',  // 后端接口地址
                    type: 'POST',
                    //contentType: 'application/json', // 关键：确保后端能解析JSON
                    data: m, // 关键：将数据转换为JSON字符串
                    dataType: 'json', // 期望后端返回JSON数据
                    async: false, // 按照图片要求设置为同步，但请注意：同步AJAX已废弃，可能导致UI卡顿
                    success: function(res) { // 使用res参数，并检查res.code
                        if(res>0){
                            alert('添加成功')
                              parent.show();
                               // 关闭弹窗
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                        }
                    },
                    error: function() {
                        layer.msg('服务器错误', {icon: 2}); // 显示服务器错误
                    }
                });
                return false; // 阻止表单跳转
            });
        });
    </script>
</body>
</html>


